# -*- coding: utf-8 -*-
import scrapy


# class CaominSpider(scrapy.Spider):
#     name = 'caomin'
#     allowed_domains = ['kytyy.com']
#     start_urls = ['http://kytyy.com/dianying/index.html']


#     def parse(self, response):
#         print(response.body.encode('utf-8'))
#         with open('./res.txt', 'w') as f:
#             # f.write(response.body)
#             f.write('response.body')

class CaominSpider(scrapy.Spider):
    name = 'caomin'
    start_urls = ['http://stackoverflow.com/questions?sort=votes']

    def parse(self, response):
        for href in response.css('.question-summary h3 a::attr(href)'):
            full_url = response.urljoin(href.extract())
            print(full_url)
            # print(response.body)
            yield scrapy.Request(full_url, callback=self.parse_question)

    def parse_question(self, response):
        # with open('./res.txt','a') as f:
        #     f.write(response.css('h1 a::text').extract()[0]+'\r\n')

        yield {
            'title': response.css('h1 a::text').extract()[0],
            'votes': response.css('.question .vote-count-post::text').extract()[0],
            'body': response.css('.question .post-text').extract()[0],
            'tags': response.css('.question .post-tag::text').extract(),
            'link': response.url,
        }